package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"ykone/models/mysql"

	"ykone/ykonerpc/internal/svc"
	"ykone/ykonerpc/ykonerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProduceUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProduceUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProduceUpdateLogic {
	return &ProduceUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProduceUpdateLogic) ProduceUpdate(in *ykonerpc.ProduceUpdateRequest) (*ykonerpc.ProduceUpdateResponse, error) {
	// todo: add your logic here and delete this line

	produceupdate := mysql.Produce{
		Model:        gorm.Model{ID: uint(in.Id)},
		ProductName:  in.ProductName,
		ProductStore: in.ProductStore,
		Price:        in.Price,
		Img:          in.Img,
	}
	if produceupdate.ID == 0 {
		return nil, fmt.Errorf("商品不存在,请重新选择")
	}
	err := mysql.DB.Debug().Model(&produceupdate).Updates(&produceupdate).Error
	if err != nil {
		return nil, fmt.Errorf("商品修改失败")
	}

	return &ykonerpc.ProduceUpdateResponse{Id: int64(produceupdate.ID)}, nil
}
